package com.eleadmin.store.service.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
//import com.eleadmin.store.mapper.SellSonMapper;
import com.eleadmin.damon.utils.utils.ArithUtil;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.store.biz.ProductStorageBiz;
import com.eleadmin.store.biz.SellBiz;
import com.eleadmin.store.entity.*;
import com.eleadmin.store.service.SellSonService;
import org.springframework.stereotype.Service;
import com.eleadmin.store.biz.SellSonBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售单子表服务实现类
 * @author : Doman
 * @date : 2024-03-07 14:15:36
 * @version ：1.0
 */
@Service
public class SellSonServiceImpl /*extends ServiceImpl<SellSonMapper, SellSon> */implements SellSonService {

    @Autowired
    private SellSonBiz sellSonBiz;
    @Autowired
    private SellBiz sellBiz;
    @Autowired
    private ProductStorageBiz productStorageBiz;

    @Override
    public JsonResult list(PageResult2<T> page, SellSon sellSon, User loginUser)  throws Exception{
        return sellSonBiz.list(page,sellSon,loginUser);
    }

    @Override
    public JsonResult list(SellSon sellSon, User loginUser) {
        return JsonResult.ok().setData(sellSonBiz.list(sellSon,loginUser));
    }

    @Override
    public JsonResult calcTotalPrice(SellSon sellSon, User loginUser) {
        return JsonResult.ok().setData(sellSonBiz.calcTotalPrice(sellSon, loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(sellSonBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(SellSon sellSon, User loginUser) {
        if (sellSonBiz.save(sellSon, loginUser)) {
            //主表 总金额更新
            Double total = sellSonBiz.calcTotalPrice(sellSon, loginUser);
            Sell sell = new Sell(sellSon.getSellid() + "", total + "");
            sellBiz.updateTotal(sell, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(sellSon.getWarehousename());
            ps.setProductname(sellSon.getProductname());
            //保存的时候有就可以了  下面哪个修改的方法code就不用加了
            ps.setCode(sellSon.getCode());
            ps.setSpecification(sellSon.getSpecification());
            ps.setLengths(sellSon.getLengths() + "");
            ps.setNegative(sellSon.getNegative() + "");
            ps.setNum(sellSon.getNum());
            ps.setLiweight(sellSon.getLiweight() + "");
            ps.setLitotalweight(sellSon.getLitotalweight());
            ps.setPrice(sellSon.getPrice() + "");
            ps.setModel(sellSon.getModel());
            ps.setLocality(sellSon.getLocality());
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            if (Utils.isNotEmpty(byName)) {
                byName.setCode(sellSon.getCode());
                byName.setNum(ArithUtil.sub(0, sellSon.getNum()));
                byName.setLitotalweight(ArithUtil.sub(0, sellSon.getLitotalweight()));
                productStorageBiz.updateNum(byName, loginUser);
            } else {
                productStorageBiz.save(ps, loginUser);
            }

            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(SellSon sellSon, User loginUser)throws Exception {
        SellSon b2 = sellSonBiz.getById(sellSon.getId());
        if (sellSonBiz.update(sellSon, loginUser)) {
            //主表 总金额更新
            Double total = sellSonBiz.calcTotalPrice(sellSon, loginUser);
            Sell sell = new Sell(sellSon.getSellid() + "", total + "");
            sellBiz.updateTotal(sell, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(sellSon.getWarehousename());
            ps.setProductname(sellSon.getProductname());
            ps.setSpecification(sellSon.getSpecification());
            ps.setLengths(sellSon.getLengths() + "");
            ps.setNegative(sellSon.getNegative() + "");
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            try {
                byName.setNum(ArithUtil.sub(b2.getNum(), sellSon.getNum()));
                byName.setLitotalweight(ArithUtil.sub(b2.getLitotalweight(),
                        sellSon.getLitotalweight()));
                productStorageBiz.updateNum(byName, loginUser);
            } catch (Exception e) {
                throw new Exception("没有对应的库存数据！");
            }

            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(SellSon sellSon, User loginUser) throws Exception{
        SellSon b2 = sellSonBiz.getById(sellSon.getId());
        if (sellSonBiz.remove(sellSon.getId(), loginUser)) {
            //主表 总金额更新
            Double total = sellSonBiz.calcTotalPrice(sellSon, loginUser);
            Sell sell = new Sell(sellSon.getSellid() + "", total + "");
            sellBiz.updateTotal(sell, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(b2.getWarehousename());
            ps.setProductname(b2.getProductname());
            ps.setSpecification(b2.getSpecification());
            ps.setLengths(b2.getLengths() + "");
            ps.setNegative(b2.getNegative() + "");
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            try {
                byName.setNum(b2.getNum());
                byName.setLitotalweight( b2.getLitotalweight());
                productStorageBiz.updateNum(byName, loginUser);
            } catch (Exception e) {
                throw new Exception("没有对应的库存数据！");
            }
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
